package b_byteStream;

import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;

/*
    常用方法
   ①.int read()
   ②.int read(byte[] b)
   ③.int read(byte[] b, int off, int len)

   ①.void write(byte[] b):写入字节数组
   ②.void write(byte[] b, int off, int len)
   ③.void write(int b):写入单个字节
 */
public class APIDemo {

    @Test //写入单个字节
    public void write01() throws IOException {
        FileOutputStream fos = new FileOutputStream("a.txt");
        //ASCII码
        fos.write(97);//a
        fos.write(65);//A
        fos.write(48);//0
        //二进制进行计算
        fos.write(-1);
        fos.write(256);
    }

    @Test//读字节
    public void read01() throws IOException {
        FileInputStream fis = new FileInputStream("a.txt");
        /*读文件 -> 读取到的是低八位的数据
            读一个字节(8位): 返回值是int类型(32位)
            将8位放入到32位中,存入到32位中的低8位,其他位全补0
         */
        /*
        int i1 = fis.read();
        System.out.println(i1);
        int i2 = fis.read();
        System.out.println(i2);
        int i3 = fis.read();
        System.out.println(i3);
        int i4 = fis.read();//写入: -1 读取:255
        System.out.println(i4);
        int i5 = fis.read();//写入:256 读取:0
        System.out.println(i5);
        //读到文件末尾返回-1
        int i6 = fis.read();
        System.out.println(i6);//-1
        */
        //定义初始值
        int i;
        //循环 -> 最终结果不要读到-1
        while((i = fis.read()) != -1){
            System.out.println(i);
        }
    }

    @Test//void write(byte[] b)
    public void write02() throws IOException {
        FileOutputStream fos = new FileOutputStream("a.txt");
        //写中文
        String str = "你好";
        byte[] bs = str.getBytes();
        //[-28, -67, -96, -27, -91, -67]
        System.out.println(Arrays.toString(bs));
        fos.write(bs);
    }

    @Test//int read(byte[] b)
    public void read02() throws IOException {
        FileInputStream fis = new FileInputStream("a.txt");
        //创建一个字节数组
        byte[] bs = new byte[1024];

        int len;
        while((len = fis.read(bs)) != -1){
            System.out.println(Arrays.toString(bs));
        }
    }


    @Test//void write(byte[] b, int off, int len)
    public void write03() throws IOException {
        FileOutputStream fos = new FileOutputStream("a.txt");
        String str = "色即是空,空即是色";
        //1字符 = 3字节,截取以字节为准
        byte[] bs = str.getBytes();
        System.out.println(Arrays.toString(bs));
        fos.write(bs,3,3);
    }


    @Test//文件的复制 -> 先读后写
    public void test() throws IOException {
        //写文件对象 - 新文件对象
        FileOutputStream fos = new FileOutputStream("a_copy.mp4");
        //读文件对象
        FileInputStream fis = new FileInputStream("a.mp4");

        /*
            存在问题: 当文件内容足够大,需要的时间多久?如何优化?
         */

        long time1 = System.currentTimeMillis();
        //读取文件的内容
        byte[] bs = new byte[1024];
        int len;//读取到字节长度
        while((len = fis.read(bs)) != -1){
            //写文件
            fos.write(bs,0,len);
        }
        long time2 = System.currentTimeMillis();
        System.out.println(time2 - time1);
    }

}
